package com.example.personal.mvvm.activity
import android.view.View
import android.widget.TextView
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.personal.R
import com.example.personal.mvvm.adapter.LegendAdapter
import com.example.personal.mvvm.bean.HalfWarningBean
import com.example.personal.mvvm.bean.LegendTextInfo
import com.example.personal.mvvm.bean.SpecialWarningBean
import com.example.personal.mvvm.bean.TrendInfo
import com.example.personal.mvvm.viewmodel.AnalyseViewModel
import com.github.mikephil.charting.data.PieEntry
import com.gsafety.libraryktx.base.BaseActivity
import com.gsafety.libraryktx.bean.AxisText
import com.gsafety.libraryktx.bean.PieChartInfo
import com.gsafety.libraryktx.core.RouterConstants
import com.gsafety.libraryktx.expand.create
import com.gsafety.libraryktx.manager.UserManager
import com.gsafety.libraryktx.util.RecyclerViewHelper
import com.gsafety.libraryktx.widget.CommonLineChart
import com.gsafety.libraryktx.widget.CommonPieChart
import kotlinx.android.synthetic.main.personal_analyse_activity.*
import kotlinx.android.synthetic.main.personal_analyse_leader.*
import kotlinx.android.synthetic.main.personal_analyse_unleader.*
import kotlin.collections.ArrayList
/**
 * 统计分析
 */
@Route(path = RouterConstants.ANALYSE_ACTIVITY)
class AnalyseActivity : BaseActivity() {
    private lateinit var mv: AnalyseViewModel
    lateinit var projectcolors: ArrayList<Int>
    override fun initView() {
        findViewById<TextView>(R.id.title_name).text=resources.getText(R.string.personal_my_analysis)
        projectcolors = ArrayList()
        projectcolors.add(R.color.common_color_ff9e35)
        projectcolors.add(R.color.common_color_f17b81)
        projectcolors.add(R.color.common_color_3dcc73)
        projectcolors.add(R.color.common_color_ff414f)
        projectcolors.add(R.color.common_color_2084ff)
        if(UserManager.getInstance().isLeader()){
            unleader.visibility= View.GONE
        }else{
            leader.visibility= View.GONE
        }
    }

    override fun initData() {
        initmodel()
        request()
    }

    override fun getLayoutId(): Int {
        return R.layout.personal_analyse_activity
    }

    //饼图添加数据
    private fun cwpiechartData(legends:List<LegendTextInfo>, pie: CommonPieChart) {
        var infos: MutableList<PieChartInfo> = ArrayList()
        legends.forEach {
            var p= PieEntry(it.number.toFloat())
            infos.add(PieChartInfo(p, resources.getColor(it.color)))
        }
        pie.setData(infos)
    }



    //近半年监测预警数量统计
    private fun hwsingleBarData(data:HalfWarningBean) {
        var xAxisValues: MutableList<AxisText> = ArrayList()
        var yAxisValues: MutableList<Float> = ArrayList()
        var colors: MutableList<Int> = ArrayList()
        data.months.forEachIndexed { index, s ->
            colors.add(resources.getColor(R.color.common_color_single_bar_bg_end))
            xAxisValues.add(AxisText(index.toFloat(), "${s}月"))
            yAxisValues.add(data.warningCount[index].toFloat())
        }
        single_bar.setsingleData(xAxisValues, yAxisValues, colors)
    }

    /**
     * 折线图
     */
    private fun lineData(datas:List<TrendInfo> , line: CommonLineChart) {
        var xAxisValues: MutableList<AxisText> = ArrayList()
        var yAxisValues: MutableList<List<Float>> = ArrayList()
        var colours: MutableList<Int> = ArrayList()
        datas.forEachIndexed { index, trendInfo ->
            var vList: MutableList<Float> = ArrayList()
            var months=trendInfo.months
            var warningCounts=trendInfo.warningCount
            xAxisValues.clear()
           months.forEachIndexed { position, s ->
               vList.add(warningCounts[position].toFloat())
               xAxisValues.add(AxisText(position.toFloat(), "${s}月"))
            }
            yAxisValues.add(vList)
            colours.add(resources.getColor(trendInfo.colors))
        }
       line.setData(xAxisValues, yAxisValues, colours)
    }


    //本月预警处置时长横向添加数据
    private fun swsingleBarLeadData(datas:List<SpecialWarningBean>) {
        var datas=datas.sortedByDescending {
         it.distimeInterval.toFloat()
        }
        var xAxisValues: MutableList<AxisText> = ArrayList()
        var yAxisValues: MutableList<Float> = ArrayList()
        var colors: MutableList<Int> = ArrayList()
        var legends:MutableList<LegendTextInfo> = ArrayList()
        datas.forEachIndexed { index, it ->
            xAxisValues.add(AxisText(index.toFloat(), "top${index+1}"))
            yAxisValues.add(it.distimeInterval.toFloat())
            legends.add(LegendTextInfo(it.specialSys,"",projectcolors[index%5],it.distimeInterval))
            colors.add(resources.getColor(projectcolors[index%5]))
        }
        xAxisValues.reverse()
        var adapter= LegendAdapter(legends)
        RecyclerViewHelper.initRecyclerViewH(this, rv_sw!!, 3, adapter)
        single_bar_lead.setsingleData(xAxisValues, yAxisValues, colors)
    }


    /**
     * 初始化viewmodel
     */
    private fun initmodel() {
        mv = AnalyseViewModel()
        mv = create(AnalyseViewModel::class)
        currenwarn()
        halfWarn()
        specialWarn()
        halfWarnTrend()
        specialTrend()
        specialStatis()
    }
//
    /**
     * 请求数据
     */
    private fun request(){
        mv.requestCurrWarning()
        mv.requestHalfWarning()
        mv.requestspecialWarning()
        mv.requesthalfTrend()
        mv.requestspecialTrend()
        mv.requestspecialStatis()
    }

    /**
     * 当前预警统计
     */
    private fun currenwarn(){
        mv.mCurrWarn.observe(this, Observer {
            if (it != null) {
                var legends =ArrayList<LegendTextInfo>()
                it.forEachIndexed { index, bean ->
                    legends.add(LegendTextInfo(bean.disposalStatus,"${bean.disposalNum}条",projectcolors[index%5],bean.disposalNum))
                }
                var adapter= LegendAdapter(legends)
                RecyclerViewHelper.initRecyclerViewH(this, rv_cw!!, 2, adapter)
                cwpiechartData(legends,chart_pie_current)
            }
        })
    }
    /*
     *近半年监测预警数量统计
     */
    private fun halfWarn(){
        mv.mHalfWarn.observe(this, Observer {
            if (it != null) {
                hwsingleBarData(it)
                }
        })
    }


    /**
     *本月监测预警处置时长统计
     */
    private fun specialWarn(){
        mv.mSpecialWarn.observe(this, Observer {
            if (it != null) {
                swsingleBarLeadData(it)
            }
        })
    }

    /**
     *近半年监测预警趋势统计
     */
    private fun halfWarnTrend(){
        mv.mHalfTrend.observe(this, Observer {
            if (it != null) {
                var trendInfos =ArrayList<TrendInfo>()
                var legends =ArrayList<LegendTextInfo>()
                it.forEachIndexed { index, data ->
                    var info=TrendInfo(data.warningLevel,data.months,data.warningCount,projectcolors[index%5])
                    trendInfos.add(info)
                    legends.add(LegendTextInfo(data.warningLevel,"",projectcolors[index%5],""))
                }
                var adapter= LegendAdapter(legends)
                RecyclerViewHelper.initRecyclerViewH(this, rv_td_hf!!, 4, adapter)
                lineData(trendInfos, multiple_line)
            }
        })
    }

    /**
     *当前各专项检测预警趋势统计
     */
    private fun specialTrend(){
        mv.mSpecialTrend.observe(this, Observer {
            if (it != null) {
                var legends =ArrayList<LegendTextInfo>()
                it.forEachIndexed { index, bean ->
                    legends.add(LegendTextInfo(bean.specialSys,"${bean.disposalNum}条",projectcolors[index%5],bean.disposalNum))
                }
                var adapter= LegendAdapter(legends)
                RecyclerViewHelper.initRecyclerViewH(this, rv_swt!!, 2, adapter)
                cwpiechartData(legends,chart_pie_special)
            }
        })
    }
    /**
     *统计专项监测预警趋势分析
     */
    private fun specialStatis(){
        mv.mSpecialStatis.observe(this, Observer {
            if (it != null) {
                var trendInfos =ArrayList<TrendInfo>()
                var legends =ArrayList<LegendTextInfo>()
                it.forEachIndexed { index, data ->
                    var info=TrendInfo(data.specialSys,data.months,data.warningCount,projectcolors[index%5])
                    trendInfos.add(info)
                    legends.add(LegendTextInfo(data.specialSys,"",projectcolors[index%5],""))
                }
                var adapter= LegendAdapter(legends)
                RecyclerViewHelper.initRecyclerViewH(this, rv_sw_ts!!, 4, adapter)
                lineData(trendInfos,project_line)
            }
        })
    }
}